import { createRouter, createWebHashHistory } from 'vue-router'
import { getToken } from '@/utils/auth'
import Login from '@/views/auth/login/login.vue'

import Layout from '@/layout'

import application from '@/views/system/application'
import User from '@/views/system/user'
import role from '@/views/system/role'
import setting from '@/views/system/setting'

const routes = [
  {
    path: '/login',
    name: 'login',
    component : Login
  },
  {
    path: '/',
    component : Layout,
    redirect : '/user',
    children : [
      {
        path: '/application',
        name: 'application',
        component : application
      },
      {
        path: '/user',
        name: 'user',
        component : User
      },
      {
        path: '/role',
        name: 'role',
        component : role
      },
      {
        path: '/setting',
        name: 'setting',
        component : setting
      },
    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

let paths = ["/login"]

router.beforeEach((to, from, next) => {
  if (paths.indexOf(to.path) != -1){
    return next()
  }
  if (!getToken()){
    return next("/login")
  }
  next()
})
export default router
